<?xml version="1.0" encoding="UTF-8"?>
<!-- $Header: /compromis/home/xmlschemata/cvs/books/relaxng/xhtml/html2db.xsl,v 1.11 2003/09/09 17:55:14 vdv Exp $ -->
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xhtml="http://www.w3.org/1999/xhtml"
	xmlns:exsl="http://exslt.org/common"
	xmlns:date="http://exslt.org/dates-and-times"
	xmlns:v="http://van-der-vlist.com/ns/h2b"
	extension-element-prefixes="exsl date"
	exclude-result-prefixes="exsl date xhtml v">
	<xsl:output method="xml" cdata-section-elements="programlisting synopsis" indent="yes" encoding="iso-8859-1"/>

	<xsl:variable name="h2">
		<xsl:choose>
			<xsl:when test="//xhtml:h1[not(@class)]">sect2</xsl:when>
			<xsl:otherwise>sect1</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<xsl:variable name="h3">
		<xsl:choose>
			<xsl:when test="//xhtml:h1[not(@class)]">sect3</xsl:when>
			<xsl:otherwise>sect2</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<v:literals>
		<v:l>author</v:l>
		<v:l>character</v:l>
		<v:l>externalRef</v:l>
		<v:l>external</v:l>
		<v:l>element-name</v:l>
		<v:l>element-born</v:l>
		<v:l>start</v:l>
		<v:l>element-character</v:l>
		<v:l>born</v:l>
		<v:l>library</v:l>
		<v:l>book</v:l>
		<v:l>id</v:l>
		<v:l>attribute-id</v:l>
		<v:l>parentRef</v:l>
		<v:l>parent</v:l>
		<v:l>element-died</v:l>
		<v:l>element-qualification</v:l>
		<v:l>ref</v:l>
		<v:l>include</v:l>
		<v:l>attribute-available</v:l>
		<v:l>available</v:l>
		<v:l>empty</v:l>
		<v:l>element-author</v:l>
		<v:l>content-person</v:l>
		<v:l>element-email</v:l>
		<v:l>choice</v:l>
		<v:l>interleave</v:l>
		<v:l>combine</v:l>
		<v:l>|=</v:l>
		<v:l>=</v:l>
		<v:l>email</v:l>
		<v:l>content-model</v:l>
		<v:l>name</v:l>
		<v:l>notAllowed</v:l>
		<v:l>died</v:l>
		<v:l>group</v:l>
		<v:l>ns</v:l>
		<v:l>div</v:l>
		<v:l>head</v:l>
		<v:l>head.attlist</v:l>
		<v:l>head.content</v:l>
		<v:l>meta</v:l>
		<v:l>foo</v:l>
		<v:l>title</v:l>
		<v:l>xsl:stylesheet</v:l>
		<v:l>xsl:template</v:l>
		<v:l>grammar</v:l>
		<v:l>define</v:l>
		<v:l>VALIDCHARS</v:l>
		<v:l>#define</v:l>
		<v:l>xmlns</v:l>
		<v:l>hr</v:l>
		<v:l>publisher</v:l>
		<v:l>dc:publisher</v:l>
		<v:l>datatypeLibrary</v:l>
		<v:l>xmlns:hr</v:l>
		<v:l>lib</v:l>
		<v:l>anything</v:l>
		<v:l>anyName</v:l>
		<v:l>*</v:l>
		<v:l>except</v:l>
		<v:l>-</v:l>
		<v:l>nsName</v:l>
		<v:l>xxx:*</v:l>
		<v:l>ns</v:l>
		<v:l>local</v:l>
		<v:l>foreign-nodes</v:l>
		<v:l>isbn</v:l>
		<v:l>mixed</v:l>
		<v:l>data</v:l>
		<v:l>foreign-attributes</v:l>
		<v:l>xml:space</v:l>
		<v:l>ID</v:l>
		<v:l>anything</v:l>
		<v:l>text</v:l>
		<v:l>x</v:l>
		<v:l>inherit</v:l>
		<v:l>p</v:l>
		<v:l>Inline.model</v:l>
		<v:l>qualification</v:l>
		<v:l>em</v:l>
		<v:l>strong</v:l>
		<v:l>library-element</v:l>
		<v:l>book-content</v:l>
		<v:l>authors</v:l>
		<v:l>characters</v:l>
		<v:l>lib:name</v:l>
		<v:l>hr:name</v:l>
		<v:l>xlink:href</v:l>
		<v:l>foreign-nodes</v:l>
		<v:l>dc:copyright</v:l>
		<v:l>##</v:l>
		<v:l>/**</v:l>
		<v:l>value</v:l>
		<v:l>param</v:l>
		<v:l>[</v:l>
		<v:l>]</v:l>
		<v:l>dc:author</v:l>
		<v:l>dc:title</v:l>
		<v:l>[ element content ]</v:l>
		<v:l>href</v:l>
		<v:l>element</v:l>
		<v:l>pattern</v:l>
		<v:l>#</v:l>
		<v:l>xhtml:p</v:l>
		<v:l>name-element</v:l>
		<v:l>xhtml:h1</v:l>
		<v:l>xhtml:div</v:l>
		<v:l>class</v:l>
		<v:l>sn:stages</v:l>
		<v:l>stageName</v:l>
		<v:l>author-or-book</v:l>
		<v:l>sn:stage</v:l>
		<v:l>a:defaultValue</v:l>
		<v:l>a:default</v:l>
		<v:l>tx:enableAbstractElements</v:l>
		<v:l>true</v:l>
		<v:l>false</v:l>
		<v:l>b</v:l>
		<v:l>rule</v:l>
		<v:l>assert</v:l>
		<v:l>report</v:l>
		<v:l>context</v:l>
		<v:l>test</v:l>
		<v:l>if:pipe</v:l>
		<v:l>if:transform</v:l>
		<v:l>if:validate</v:l>
		<v:l>if:ignore</v:l>
		<v:l>@</v:l>
		<v:l>default</v:l>
		<v:l>xsd:date</v:l>
		<v:l>eg:occurs</v:l>
		<v:l>?</v:l>
		<v:l>id</v:l>
		<v:l>{dtd:id}</v:l>
		<v:l>src:fragment</v:l>
		<v:l>src:fragref</v:l>
		<v:l>dtd:ID</v:l>
		<v:l>type</v:l>
		<v:l>token</v:l>
		<v:l>attribute</v:l>
		<v:l>oneOrMore</v:l>
		<v:l>zeroOrMore</v:l>
		<v:l>optional</v:l>
		<v:l>list</v:l>
		<v:l>xmlns</v:l>
		<v:l>bar</v:l>
		<v:l>span</v:l>
		<v:l>xml:lang</v:l>
		<v:l>span.attlist</v:l>
		<v:l>dc:Rights</v:l>
		<v:l>title.content</v:l>
		<v:l>second</v:l>
		<v:l>first</v:l>
		<v:l>third</v:l>
		<v:l>pattern1</v:l>
		<v:l>pattern2</v:l>
		<v:l>0</v:l>
		<v:l>1</v:l>
		<v:l>xsd:boolean</v:l>
		<v:l>integer</v:l>
		<v:l>boolean</v:l>
		<v:l>document()</v:l>
		<v:l></v:l>
		<v:l></v:l>
		<v:l></v:l>
		<v:l></v:l>
		<v:l></v:l>
	</v:literals>

	<xsl:template match="/">
		<xsl:processing-instruction name="xml-stylesheet">type="text/css" href="chapter.css"</xsl:processing-instruction>
		<xsl:apply-templates select="/xhtml:html/xhtml:body/xhtml:div[@id='body']"/>
	</xsl:template>

	<xsl:template name="nextStep">
		<xsl:apply-templates select="following-sibling::*[1]" mode="stepByStep"/>
	</xsl:template>
	
	<xsl:template match="xhtml:h2|xhtml:h3|xhtml:h1[not(@class)]" mode="stepByStep"/>
	
	<xsl:template match="xhtml:div[@id='body']">
		<chapter>
			<xsl:apply-templates select="xhtml:h1[@class='divTitle']" mode="title"/>
			<simplesect>
				<xsl:apply-templates select="*[1]" mode="stepByStep"/>
			</simplesect>
			<xsl:choose>
				<xsl:when test="xhtml:h1[not(@class)]">
					<xsl:apply-templates select="xhtml:h1[not(@class)]"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates select="xhtml:h2"/>
				</xsl:otherwise>
			</xsl:choose>
		</chapter>
	</xsl:template>

	<xsl:template match="xhtml:h1">
		<sect1>
			<title><xsl:apply-templates/></title>
			<xsl:call-template name="nextStep"/>
			<xsl:variable name="pos" select="count(preceding-sibling::xhtml:h1)"/>
			<xsl:apply-templates select="following-sibling::xhtml:h2[count(preceding-sibling::xhtml:h1) = $pos + 1]"/>
		</sect1>
	</xsl:template>

	<xsl:template match="xhtml:h2">
		<xsl:element name="{$h2}">
			<title><xsl:apply-templates/></title>
			<xsl:call-template name="nextStep"/>
			<xsl:variable name="pos" select="count(preceding-sibling::xhtml:h2)"/>
			<xsl:apply-templates select="following-sibling::xhtml:h3[count(preceding-sibling::xhtml:h2) = $pos + 1]"/>
		</xsl:element>
	</xsl:template>

	<xsl:template match="xhtml:h3">
		<xsl:element name="{$h3}">
			<title><xsl:apply-templates/></title>
			<xsl:call-template name="nextStep"/>
		</xsl:element>
	</xsl:template>

	<xsl:template match="xhtml:h1[@class='divTitle']" mode="stepByStep">
		<xsl:call-template name="nextStep"/>
	</xsl:template>

	<xsl:template match="xhtml:p[xhtml:img]" mode="stepByStep">
		<xsl:apply-templates/>
		<xsl:call-template name="nextStep"/>
	</xsl:template>

	<xsl:template match="xhtml:p" mode="stepByStep">
		<para>
				<xsl:apply-templates/>
		</para>
		<xsl:call-template name="nextStep"/>
	</xsl:template>

	<xsl:template match="xhtml:pre" mode="stepByStep">
		<programlisting>
				<xsl:apply-templates />
		</programlisting>
		<xsl:call-template name="nextStep"/>
	</xsl:template>

	<xsl:template match="xhtml:img">
		<figure id="_{substring-before(@src, '.')}">
			<title>
				<xsl:value-of select="translate(substring-before(@src, '.'), '_', ' ')"/>
			</title>
			<graphic fileref="{@src}"/>
		</figure>
	</xsl:template>

	<xsl:template match="xhtml:ul" mode="stepByStep">
		<itemizedlist>
				<xsl:apply-templates/>
		</itemizedlist>
		<xsl:call-template name="nextStep"/>
	</xsl:template>

	<xsl:template match="xhtml:li" >
		<listitem>
			<para>
				<xsl:apply-templates/>
			</para>
		</listitem>
	</xsl:template>

	<xsl:template match="xhtml:h1[@class='divTitle']" mode="title">
		<title>
			<xsl:apply-templates/>
		</title>
	</xsl:template>

	<xsl:template match="xhtml:pre/text()">
		<xsl:copy-of select="."/>
	</xsl:template>

	<xsl:template match="text()" name="text">
		<xsl:param name="str" select="."/>
		<xsl:choose>
			<xsl:when test="contains($str, '&quot;')">
				<xsl:variable name="before" select="substring-before($str, '&quot;')"/>
				<xsl:variable name="in-and-after" select="substring-after($str, '&quot;')"/>
				<xsl:variable name="in" select="substring-before($in-and-after, '&quot;')"/>
				<xsl:variable name="after" select="substring-after($in-and-after, '&quot;')"/>
				<xsl:choose>
					<xsl:when test="not(contains($in-and-after, '&quot;'))">
						<xsl:copy-of select="$str"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:copy-of select="$before"/>
						<xsl:choose>
							<xsl:when test="not(document('')/xsl:stylesheet/v:literals/v:l=$in)">
								<xsl:text>"</xsl:text>
								<xsl:copy-of select="$in"/>
								<xsl:text>"</xsl:text>
							</xsl:when>
							<xsl:otherwise>
								<literal>
									<xsl:copy-of select="$in"/>
								</literal>
							</xsl:otherwise>
						</xsl:choose>
						<xsl:call-template name="text">
							<xsl:with-param name="str" select="$after"/>
						</xsl:call-template>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:otherwise>
				<xsl:copy-of select="$str"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

</xsl:stylesheet>
